#include "meshes.h"

#include <memory.h>

//these are some sample implementation of custom MeshSource subclasses
//intended to show how simple and flexible it is to build them

using namespace Kraw;

bool TriangleMesh::Load()
{
    numStreams = 2;
    pStreams = new Stream[numStreams];
    memset( pStreams, 0, numStreams * sizeof(Stream) );
    
    //create streams
    pStreams[0].size = 3;
    pStreams[0].usage = Coord;
    
    pStreams[1].size = 1;
    pStreams[1].usage = Diffuse;
    
    int vertexSize = 4;//3+1
    
    numVertices = 3;
    pVertices = new float[numVertices * vertexSize];
    
    numIndices = 3;
    pIndices = new int[numIndices];
    
    float pVertexData[][4] = {
        { -1.0f, -1.0f, 0.0f, 0xffff0000, },
        {  1.0f, -1.0f, 0.0f, 0xff0000ff, },
        {  0.0f,  1.0f, 0.0f, 0xffffffff, }, };
        
    memcpy( pVertices, pVertexData, numVertices * vertexSize * sizeof(float) );
    
    pIndices[0] = 0;
    pIndices[1] = 1;
    pIndices[2] = 2;
    
    return true;
}

bool CubeMesh::Load()
{
    //build the vertices and indices of a unit cube with texcoords and normals
    numStreams = 3;
    pStreams = new Stream[numStreams];
    memset( pStreams, 0, 3 * sizeof(Stream) );
    
    //create streams
    pStreams[0].size = 3;
    pStreams[0].usage = Coord;

    pStreams[1].size = 2;
    pStreams[1].usage = TexCoord;
    
    pStreams[2].size = 3;
    pStreams[2].usage = Normal;
    
    int vertexSize = 8; //3+3+2
    
    //create vertex data
    numVertices = 24;
    pVertices = new float[numVertices * vertexSize];
    
    numIndices = 36;
    pIndices = new int[numIndices];
#if 1
    float pVertexData[][8] = {
        {-0.500000, -0.500000, -0.500000, 0.000000, -1.000000, 0.000000, 1.000000, -1.000000},
        {0.500000, -0.500000, -0.500000, 0.000000, -1.000000, 0.000000, 0.000000, -1.000000},
        {0.500000, -0.500000, 0.500000, 0.000000, -1.000000, 0.000000, 0.000000, 0.000000},
        {-0.500000, -0.500000, 0.500000, 0.000000, -1.000000, 0.000000, 1.000000, 0.000000},
        {-0.500000, 0.500000, 0.500000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000},
        {0.500000, 0.500000, 0.500000, 0.000000, 1.000000, 0.000000, 1.000000, 0.000000},
        {0.500000, 0.500000, -0.500000, 0.000000, 1.000000, 0.000000, 1.000000, -1.000000},
        {-0.500000, 0.500000, -0.500000, 0.000000, 1.000000, 0.000000, 0.000000, -1.000000},
        {-0.500000, -0.500000, 0.500000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000},
        {0.500000, -0.500000, 0.500000, 0.000000, 0.000000, 1.000000, 1.000000, 0.000000},
        {0.500000, 0.500000, 0.500000, 0.000000, 0.000000, 1.000000, 1.000000, -1.000000},
        {-0.500000, 0.500000, 0.500000, 0.000000, 0.000000, 1.000000, 0.000000, -1.000000},
        {0.500000, -0.500000, 0.500000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000},
        {0.500000, -0.500000, -0.500000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000},
        {0.500000, 0.500000, -0.500000, 1.000000, 0.000000, 0.000000, 1.000000, -1.000000},
        {0.500000, 0.500000, 0.500000, 1.000000, 0.000000, 0.000000, 0.000000, -1.000000},
        {-0.500000, -0.500000, -0.500000, 0.000000, 0.000000, -1.000000, 1.000000, 0.000000},
        {-0.500000, 0.500000, -0.500000, 0.000000, 0.000000, -1.000000, 1.000000, -1.000000},
        {0.500000, 0.500000, -0.500000, 0.000000, 0.000000, -1.000000, 0.000000, -1.000000},
        {0.500000, -0.500000, -0.500000, 0.000000, 0.000000, -1.000000, 0.000000, 0.000000},
        {-0.500000, -0.500000, 0.500000, -1.000000, 0.000000, 0.000000, 1.000000, 0.000000},
        {-0.500000, 0.500000, 0.500000, -1.000000, 0.000000, 0.000000, 1.000000, -1.000000},
        {-0.500000, 0.500000, -0.500000, -1.000000, 0.000000, 0.000000, 0.000000, -1.000000},
        {-0.500000, -0.500000, -0.500000, -1.000000, 0.000000, 0.000000, 0.000000, 0.000000} };

    memcpy( pVertices, pVertexData, numVertices * vertexSize * sizeof(float) );

    int pIndexData[][3] = {
        {3,  0,  1 },
        {1,  2,  3 },
        {4,  5,  6 },
        {6,  7,  4 },
        {8,  9,  10},
        {10, 11, 8 },
        {12, 13, 14},
        {14, 15, 12},
        {19, 16, 17},
        {17, 18, 19},
        {23, 20, 21},
        {21, 22, 23} };

    memcpy( pIndices, pIndexData, numIndices * sizeof(int) );
    
#else
    float * curVertex = (float *) pVertices;
    for (int vertexIdx = 0; vertexIdx < numVertices; ++vertexIdx)
    {
        //coords
        curVertex[0] = vertexIdx & 1 ? 1.f : -1.f;
        curVertex[1] = vertexIdx & 2 ? 1.f : -1.f;
        curVertex[2] = vertexIdx & 4 ? 1.f : -1.f;
        
        //texcoords
        curVertex[3] = vertexIdx & 1 ? 1.f : 0.f;
        curVertex[4] = vertexIdx & 2 ? 1.f : 0.f;
        
        //normals
        curVertex[5] = 0.f;
        curVertex[6] = 0.f;
        curVertex[7] = 0.f;
        
        curVertex += vertexSize;
    }
    
    int indexIdx = 0;
    int curIndex = 0;
    for (int faceIdx = 0; faceIdx < 6; ++faceIdx)
    {
        //each face has 6 indices in the same configuration
        int index0 = curIndex;
        int index1 = curIndex + 1;
        int index2 = curIndex + 2;
        int index3 = curIndex + 3;
        
        pIndices[indexIdx++] = index0;
        pIndices[indexIdx++] = index1;
        pIndices[indexIdx++] = index2;

        pIndices[indexIdx++] = index2;
        pIndices[indexIdx++] = index3;
        pIndices[indexIdx++] = index0;
        
        curIndex += 4;
    }
#endif
    return true;
}

///Load a N2 mesh
N3D2Mesh::N3D2Mesh(const char* filename)
{
    strFilename = _strdup(filename);
}

///Load a N2 mesh
bool N3D2Mesh::Load()
{
    //TODO-
    return false;
}
